/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.demo.api.das;

import cn.hallele.demo.application.action.post.PostAppService;
import cn.hallele.demo.application.bo.PostBO;
import cn.hallele.ms.demo.api.dto.TypiDTO;
import cn.hallele.ms.infrastructure.core.convertor.das.ModelDas;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * TypiDTO - TypicodeBO转换器
 *
 * @author anle5
 * @since 2024/9/15 16:37
 */
@Slf4j
@Component
public class PostDBDas extends ModelDas<PostAppService, TypiDTO, PostBO> {
    public PostDBDas(PostAppService option) {
        super(option);
    }

    public List<TypiDTO> querySomebodyPosts(int id) {
        return revertList(this.option.querySomebodyPosts(id));
    }

    /**
     * 将T转成E
     *
     * @param typiDTO 被转换者
     * @return E 转换后者
     */
    @Override
    protected PostBO convert(TypiDTO typiDTO) {
        return PostBO.builder()
                .id(typiDTO.getId())
                .text(typiDTO.getText())
                .userId(typiDTO.getUserId())
                .postId(typiDTO.getPostId())
                .title(typiDTO.getTitle())
                .body(typiDTO.getBody())
                .safe(true)
                .build();
    }

    /**
     * 将E回转陈T
     *
     * @param postBO 被回转换者
     * @return E 回转换后者
     */
    @Override
    protected TypiDTO revert(PostBO postBO) {
        return TypiDTO.builder()
                .id(postBO.getId())
                .text(postBO.getText())
                .userId(postBO.getUserId())
                .postId(postBO.getPostId())
                .title(postBO.getTitle())
                .body(postBO.getBody())
                .build();
    }
}
